package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Value("${aliyun.oss.endpoint}")
    String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    String bucketName;

    @Autowired
    DtsStorageMapper storageMapper;

    /**
     * 上传图片
     * @param filename--原始名称
     * @param size--大小
     * @param type--类型
     * @param objectName--唯一名称
     * @param bytes--字节数组
     * @return
     */
    @Override
    public DtsStorage uploadFile(String filename, Integer size, String type, String objectName, byte[] bytes) throws Exception {
        //上传图片到OSS
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, objectName, bytes);
        //存储到数据库
        DtsStorage storage = new DtsStorage();
        storage.setSize(size);
        storage.setType(type);
        storage.setKey(objectName);
        storage.setUrl(url);
        storage.setName(filename);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setDeleted(false);

        storageMapper.insert(storage);

        return storage;
    }

    /**
     * 查找每页
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @param key
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, String key) {
        IPage<DtsStorage> iPage = new Page<>(page,limit);

        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("`name`",name);
        }
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("`key`",key);
        }

        //判断降序升序
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        storageMapper.selectPage(iPage,wrapper);

        //封装结果集
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }

    @Override
    public void delete(DtsStorage storage) {
        //删除数据库
        storageMapper.deleteById(storage.getId());
        //删除阿里云
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,storage.getKey());
    }
}
